package com.zijie.shoprbacapi.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.core.util.JsonResult;
import com.zijie.rbac.model.Role;
import com.zijie.rbac.model.UserGroup;
import com.zijie.rbac.service.UserGroupService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/userGroup")
public class UserGroupApi {
    private UserGroupService userGroupService;

    @Autowired
    public void setUserGroupService(UserGroupService userGroupService) {
        this.userGroupService = userGroupService;
    }
    @GetMapping("/findAll")
    public ResponseEntity<JsonResult<?>> findAll( @RequestParam(defaultValue = "1")Integer pageNo,
                                    @RequestParam(defaultValue = "10")Integer pageSize, UserGroup usb){
        Page<UserGroup> page = Page.of(pageNo, pageSize);
//        System.out.println(pageNo+"  "+pageSize+" "+usb.toString());
        page=userGroupService.findAll(page, usb);
        return ResponseEntity.ok(JsonResult.success( page));
    }
    @PostMapping("/add")
    public ResponseEntity<JsonResult<?>> add(@RequestBody  UserGroup userGroup){
        userGroupService.add(userGroup);
        return ResponseEntity.ok(JsonResult.success("添加成功"));
    }
    @PutMapping("/update")
    public ResponseEntity<JsonResult<?>> update(@RequestBody UserGroup userGroup){
        userGroupService.update(userGroup);
        return ResponseEntity.ok(JsonResult.success("更新成功"));
    }
    //单个删除
    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity<JsonResult<?>> deleteById(@PathVariable Integer id){
        int count = userGroupService.deleteById(id);
        return ResponseEntity.ok(JsonResult.success("删除成功"));
    }
    //批量删除
    @DeleteMapping("/deleteByIds")
    public ResponseEntity<JsonResult<?>> deleteByIds(@RequestBody List<Integer> ids){
        int count = userGroupService.deleteByIds(ids);
        return ResponseEntity.ok(JsonResult.success("删除成功"));
    }
    //为用户分配用户组
    @PutMapping("/assignAdmin")
    public ResponseEntity<JsonResult<?>> assignAdminToGroup(
            @RequestParam("adminId") Long adminId,
            @RequestParam("userGroupId") Long userGroupId) {
            // 调用服务层执行分配操作
            boolean success = userGroupService.assignAdminToGroup(adminId, userGroupId);
            if (success) {
                return ResponseEntity.ok(JsonResult.success("分配成功"));
            } else {
                return ResponseEntity.ok(JsonResult.error("分配失败"));
            }

    }

    /**
     * 为用户组分配角色
     *
     * @param userGroupId 用户组ID
     * @param roleId 角色ID
     * @return 统一响应结果
     */
    @PutMapping("/assignRole")
    public ResponseEntity<?> assignRoleToUserGroup(
            @RequestParam Long userGroupId,
            @RequestParam Long roleId) {
            // 执行分配操作
            userGroupService.assignRole(userGroupId, roleId);
            return ResponseEntity.ok(JsonResult.success("分配成功"));
    }

}
